package com.jshoperxms.controller.mall.backstage.user;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.utils.SHA1;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.RedisKey;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.BasicUserT;
import com.jshoperxms.redis.RedisBaseOperation;
import com.jshoperxms.service.BasicUserTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 
* @ClassName: BasicUserT 
* @Description: 后台基础用户操作
* @author jcchen
* @date 2015年10月28日 下午4:41:58 
*
 */
@Controller
@RequestMapping("/mall/user")
public class BasicUserTController extends BaseTController {
	@Resource
	private BasicUserTService basicUserTService;
	@Resource
	private RedisBaseOperation<BasicUserT>redisBaseOperation;
	private Map<String, Object> responsedata = new HashMap<>();//数据形式

	/**
	 * 后台管理者登录
	 * @return
	 */
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity login(HttpServletRequest request, @RequestBody BasicUserT bt){
		if (StringUtils.isBlank(bt.getUsername())) {
			responsedata.put("msg",WarnWrods.EMPTY_LOGINNAME);
			responsedata.put("sucflag", false);
		}
		if (StringUtils.isBlank(bt.getPassword())) {
			responsedata.put("msg",WarnWrods.EMPTY_LOGINPWD);
			responsedata.put("sucflag", false);
		}
		BasicUserT basicUserT=new BasicUserT();
		basicUserT.setUsername(StringUtils.trim(bt.getUsername()));
		basicUserT.setPassword(SHA1.getDigestOfString(bt.getPassword()));
		basicUserT.setUserstatus(BaseEnums.UserState.ACTIVE.getState());
		basicUserT.setStatus(BaseEnums.DataUsingState.USING.getState());
		Map<String,String>params=new HashMap<String,String>();
		params.put("username", StringUtils.trim(bt.getUsername()));
		params.put("password", SHA1.getDigestOfString(bt.getPassword()));
		params.put("userstatus",BaseEnums.UserState.ACTIVE.getState());
		params.put("status", BaseEnums.DataUsingState.USING.getState());
		Criterion criterion=Restrictions.allEq(params);
		basicUserT=this.basicUserTService.findOneByCriteria(BasicUserT.class, criterion);
		if(basicUserT!=null){
			doSysIndexInit(basicUserT,request);
			responsedata.put("sucflag", true);
		}else{
			responsedata.put("msg",WarnWrods.EMPTY_ACCOUNT);
		}
		return Response.success(responsedata);
	}
	
	/**
	 * 进行用户登录有的系统首页数据初始化及用户权限相关初始化
	 */

	private void doSysIndexInit(BasicUserT basicUserT,HttpServletRequest request) {
		request.getSession().setAttribute(StaticKey.BACK_USER_SESSION_KEY, basicUserT);
		responsedata.put("token",basicUserT.getId());
		responsedata.put("bean",basicUserT);
		redisBaseOperation.put(basicUserT.getId(),basicUserT,RedisKey.MEMBERTIMEOUTTHREEM, TimeUnit.MINUTES);
	}
	
	
	
	



	
	
	
}
